package pt.ipleiria.plp.ui;

import de.enough.polish.ui.Alert;
import de.enough.polish.ui.AlertType;
import de.enough.polish.ui.Command;
import de.enough.polish.ui.CommandListener;
import de.enough.polish.ui.Displayable;
import de.enough.polish.ui.Form;
import de.enough.polish.ui.Item;
import de.enough.polish.ui.ItemStateListener;
import de.enough.polish.ui.TextField;
import de.enough.polish.ui.UiAccess;
import de.enough.polish.util.Locale;
import java.io.IOException;

import pt.ipleiria.plp.BlackSpotterMidlet;
import pt.ipleiria.plp.threads.CallbackValidKey;

/**
 *
 * @author Ricardo
 */
public class AppKeyScreen extends Form implements ItemStateListener, CommandListener,
        CallbackValidKey {

    public static final int MAX_APP_KEY_SIZE = 20;
    BlackSpotterMidlet midlet;
    private Command cmdOK = new Command(Locale.get("cmd.OK"), Command.CANCEL, 1);
    private Command cmdSim = new Command(Locale.get("cmd.Sim"), Command.CANCEL, 2);
    private Command cmdNao = new Command(Locale.get("cmd.Nao"), Command.BACK, 2);
    private Command cmdContinue = new Command(Locale.get("cmd.Continuar"), Command.CANCEL, 1);
    private Command cmdExit = new Command(Locale.get("cmd.Sair"), Command.EXIT, 2);
    private boolean cont = true;
    //#style itemInput
    private TextField appField = new TextField(Locale.get("label.AppKeyLabel"),
            "", MAX_APP_KEY_SIZE, TextField.ANY);
    private String app;

    public AppKeyScreen(BlackSpotterMidlet midlet) {
        //#style mainScreen
        super(Locale.get("title.InserirChave"));
        this.midlet = midlet;

        this.setCommandListener(this);
        this.setItemStateListener(this);
        this.addCommand(cmdContinue);
        this.addCommand(cmdExit);

        this.append(appField);
    }

    public void itemStateChanged(Item item) {
        if (item == appField) {
            if (appField.getString().length() != 0) {
                if (cont) {
                    this.removeCommand(cmdContinue);
                    this.addCommand(cmdOK);
                    appField.setDefaultCommand(cmdOK);
                    cont = false;
                }
            } else {
                if (!cont) {
                    this.removeCommand(cmdOK);
                    this.addCommand(cmdContinue);
                    appField.setDefaultCommand(cmdContinue);
                    cont = true;
                }
            }

        }
    }

    public void commandAction(Command c, Displayable d) {

        if (c == cmdExit) {
            midlet.destroyApp(false);
        }
        if (c == cmdOK) {
            app = appField.getString();

            midlet.getApp().validateAppKey(app, this);
            //#style popupAlert
            Alert alert = new Alert(Locale.get("title.serverContact"),
                    Locale.get("mensagem.serverContact"), null, AlertType.INFO);
            midlet.getDisplay().setCurrent(alert, this);

            this.removeAllCommands();
        }
        if (c == cmdContinue) {
            //#style popupAlert
            Alert alert = new Alert(Locale.get("title.noAppKey"), Locale.get("mensagem.noAppKey"), null, AlertType.CONFIRMATION);
            alert.addCommand(cmdNao);
            alert.addCommand(cmdSim);
            alert.setCommandListener(this);
            midlet.getDisplay().setCurrent(alert);
        }
        if (c == cmdNao) {
            midlet.getDisplay().setCurrent(this);
        }
        if (c == cmdSim) {
            midlet.getDisplay().setCurrent(midlet.getMainMenu());
        }
    }

    public void returnValidation(boolean valid) {
        if (valid) {
            try {
                midlet.getApp().setAppKey(app);
                midlet.goToMenu();
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        } else {
            this.addCommand(cmdOK);
            this.addCommand(cmdExit);
            //#style popupAlert
            Alert alert = new Alert(Locale.get("title.Erro"),
                    Locale.get("mensagem.appInvalida"), null, AlertType.ERROR);
            midlet.getDisplay().setCurrent(alert, this);

        }
    }
}
